/**
 * @filename:LogininforServiceImpl 2022-3-2
 * @project Wonder Java前后台分离框架  1.1.0
 * Copyright(c) 2022 Wonder Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.admin.service.impl;

import com.wonder.admin.domain.Logininfor;
import com.wonder.admin.mapper.LogininforDao;
import com.wonder.admin.service.LogininforService;
import com.wonder.admin.request.LogininforRequest;
import com.wonder.common.utils.StringUtils;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.common.AjaxMap;
import java.util.Map;
import java.util.HashMap;
import java.util.List;

import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.Date;

/**   
 * @Description:TODO(登录日志服务实现)
 *
 * @version: 1.1.0
 * @author: Wonder
 * 
 */
@Service
public class LogininforServiceImpl  extends ServiceImpl<LogininforDao, Logininfor> implements LogininforService  {

    /**
     * 验证登录日志信息
     * 
     * @param Logininfor信息
     * @return 结果
     */
    @Override
    public String validate(Logininfor logininfor,String type){
    
       if(logininfor.getBrowser()==null) return "浏览器类型"+MessageUtils.message("no.null");	
       if(type.equals("new")){     
          if(this.count(new QueryWrapper<Logininfor>().eq("browser", logininfor.getBrowser())) >0)
    	   return "浏览器类型"+MessageUtils.message("not.unique"); 
    	   }  
       if(type.equals("update")){     
          if(this.count(new QueryWrapper<Logininfor>().eq("browser", logininfor.getBrowser())
          .ne("info_id",logininfor.getInfo_id())) >0)
    	   return "浏览器类型"+MessageUtils.message("not.unique"); 
    	   } 	    
    
       return "";
    }
    
    /**
	 * Description:登录日志分页返回
	 * @request:检索条件
	 * @page：分页参数
	 * @return:分页记录
	 */
	@Override 
	public IPage<Logininfor> selectList(LogininforRequest request, Page<Logininfor> page){
	    LambdaQueryWrapper<Logininfor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
	    
	    if (null != request.getInfo_id()) {
	       lambdaQueryWrapper.eq(Logininfor::getInfo_id, request.getInfo_id());
	      }               
             
        if (StringUtils.hasText(request.getLogin_name())) {
			    lambdaQueryWrapper.like(Logininfor::getLogin_name, request.getLogin_name()); 
		    } 
             
	    if (null != request.getStatus()) {
	       lambdaQueryWrapper.eq(Logininfor::getStatus, request.getStatus());
	      }               
             
        if (request.getLogin_time() != null){
        	Date[] login_time=request.getLogin_time();
        	if(login_time.length>0)
        	  lambdaQueryWrapper.ge(Logininfor::getLogin_time, login_time[0]); 
        	if(login_time.length==2)
        	  lambdaQueryWrapper.le(Logininfor::getLogin_time,login_time[1]);      
        	
        }    
             
        
		lambdaQueryWrapper.orderByDesc(Logininfor::getInfo_id);	
		Page<Logininfor> selectPage = this.baseMapper.selectPage(page, lambdaQueryWrapper);

		return selectPage;
    }
        
	/**
	 * 导出数据
	 */
	@Override  
	public Map<String,Object> dumpdata(LogininforRequest request){
      Page<Logininfor> page = new Page<Logininfor>(request.getPage(), request.getLimit());
	  IPage<Logininfor> list =selectList(request, page);
	  String header="ID,登录账号,登录IP地址,登录地点,浏览器类型,操作系统,登录状态,提示消息,访问时间";
	  Map<String,Object> retMap=new HashMap<String, Object>();
	  retMap.put("header", StringUtils.split(header, ','));
	  retMap.put("filename", "登录日志.xlsx");
	  retMap.put("percentage", (int)list.getCurrent()*100/list.getPages());
	  retMap.put("data", list.getRecords());
	  return AjaxMap.success(retMap);
	}
	
    
	
}